package com.eve.eplus.sns;

import java.util.List;

import com.eve.eplus.common.db.PersistentUtils;
import com.eve.eplus.model.ContentModel;

/**
 * 用于管理SNS时间线内容
 * 
 * @author norsie
 * @create 13-04-08
 */
public class SNSContentManager
{
    // private Context mContext;
    // private AccountModel accountModel;

    public SNSContentManager()
    {
        // this.mContext = context;
    }

    /**
     * 储存内容信息
     * 
     * @param accountInfo
     */
    public static void save(String owner_uid, String json_text)
    {
        ContentModel contentModel = new ContentModel();
        contentModel.setOwner_uid(owner_uid);
        contentModel.setJson(json_text);
        if (!findRecord(owner_uid))

            PersistentUtils.addModel(contentModel);
        else
        {
            contentModel.setId(getModel(owner_uid).getId());
            PersistentUtils.update(contentModel);
        }

    }

    /**
     * 通过owner_uid查询记录是否存在
     * 
     * @param uid
     *            uid信息
     * @return
     */
    public static boolean findRecord(String uid)
    {

        List<ContentModel> list = PersistentUtils.getModelList(
                ContentModel.class, "owner_uid='" + uid + "'");
        if (list.isEmpty())
            return false;
        else
            return true;

    }

    /**
     * 从数据库读取内容信息
     */
    public static String getJsonInfo(String uid)
    {
        List<ContentModel> list = PersistentUtils.getModelList(
                ContentModel.class, "owner_uid='" + uid + "'");
        if (!list.isEmpty())
            return list.get(0).getJson();
        return null;
    }

    /**
     * 获得内容信息模型
     */

    public static ContentModel getModel(String uid)
    {
        List<ContentModel> list = PersistentUtils.getModelList(
                ContentModel.class, "owner_uid='" + uid + "'");
        if (!list.isEmpty())
            return list.get(0);
        return null;
    }

    /**
     * 删除模型
     */
    public static void delete(String uid)
    {
        PersistentUtils.delete(getModel(uid));

    }
}
